package com.h3c.gm.common.utils

import com.google.common.collect.Lists
import com.google.common.collect.Maps
import org.joda.time.DateTime

import java.text.SimpleDateFormat

/**
 * Created by d14287 on 2017/7/1.
 */
class ControllerUtils {

    /**
     *
     * @param from
     * @param to
     * @param contents 需要往content中添加的元素
     * @return
     */
    static List<Map> initMonthStatistics(DateTime from, DateTime to, List<String> contents) {
        def list = Lists.newArrayList()
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM')
        while (from.getYear() != to.getYear() || from.getMonthOfYear() != to.getMonthOfYear()) {
            def map = Maps.newHashMap()
            map.put('m', sdf.format(from.toDate()))
            contents.each {
                item ->
                    map.put(item, 0)
            }
            list.add(map)
            from = from.plusMonths(1)
        }
        //补上当前月
        def map = Maps.newHashMap()
        map.put('m', sdf.format(to.toDate()))
        contents.each {
            item ->
                map.put(item, 0)
        }
        list.add(map)

        return list
    }
}
